const routerPath = {
    'login': '/pages/login/login',
    'index': '/pages/index/index',
    'socket': '/pages/socket/socket',
    'company': '/pages/company-list/company',
    'order': '/pages/order/idnex',
    'order-all': '/pages/order-all/order-all',
    'lane-detail': '/pages/lane-detail/lane-detail',
    'select-date': '/pages/select-date/select-date',
    'profile': '/pages/profile/index',
    'carlist': '/pages/car-list/index',
    'cardetail': '/pages/car-detail/index',
    'fault-list': '/pages/fault-list/index',
    'xieyi': '/pages/xieyi/index'
}

module.exports = {
    push(path, option = {}) {
        if(typeof path === 'string') {
            option.path = path
        } else {
            option = path
        }

        let url = routerPath[option.path]
        let { query = {}, openType, duration } = option
        let params = this.parse(query)
        if (params) {
            url+='?' + params
        }
        duration ? setTimeout(() => {
            this.to(openType, url)
        }, duration): this.to(openType, url)
    },

    to(openType, url) {
        let obj = {url}
        if (openType === 'redirect') {
            wx.redirectTo(obj)
        } else if (openType === 'reLaunch') {
            wx.reLaunch(obj)
        } else if (openType === 'back') {
            wx.navigateBack({
                delta:1
            })
        } else {
            wx.navigateTo(obj)
        }
    },

    parse(data) {
        let arr = []
        for (let key in data) {
            arr.push(key+ '=' + data[key])
        }
        return arr.join('&')
    }
}
